/*
 * Copyright 2006-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package asphalt2.control
{
	import asphalt2.util.UIUtil;
	
	import flash.events.EventDispatcher;
	
	import mx.core.Container;
	import mx.core.IMXMLObject;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	/**
	 * @author c9katayama
	 * @author shot
	 **/	
	public dynamic class AbstractControl extends EventDispatcher implements IControl, IMXMLObject
	{
		private var _ui:Container;
		
		public var uiPropertyName:String = "page";
		
		public function get UI():Container{
			return _ui
		}
		
		public function set UI(ui:Container):void{
			this._ui = ui;
		}
		
		public function initialized(document:Object, id:String):void{
			UI = document as Container;

			injectionProperty();
			
			UIUtil.addOnceEventListener(UI,FlexEvent.PREINITIALIZE,function(e:FlexEvent):void{
				preInit();
			});
			UIUtil.addOnceEventListener(UI,FlexEvent.INITIALIZE,function(e:FlexEvent):void{
				postInit();
			});
			UIUtil.addOnceEventListener(UI,FlexEvent.CREATION_COMPLETE,function(e:FlexEvent):void{
				setup();
			});
			UIUtil.callBackWhenComponentRemoved(UI,function():void{
				_ui = null;
			})
		}
		/**
		 * uiPropertyNameと同じ名前のプロパティがある場合、UIComponentをセットします。
		 */
		protected function injectionProperty():void{
			if(hasOwnProperty(uiPropertyName)){
				this.page = UI;
			}
		}
		/**このメソッドをオーバーライドすることで、UI初期化に処理を行えます。*/
		public function preInit():void
		{
		}
		/**このメソッドをオーバーライドすることで、UI初期化後に処理を行えます。*/
		public function postInit():void
		{
		}
		/**このメソッドをオーバーライドすることで、UI作成が全ておわったあとに初期化を行えます。*/
		public function setup():void
		{
		}
	}
}